Skip to content

Conversation

@jinzhongjia
Copy link
Member

@jinzhongjia jinzhongjia commented Apr 28, 2025

Use zig's compile-time features to bring a stronger reflection experience

This allows us to get the parameters of the webui more freely!

/// binding function: Creates a binding between an HTML element and a callback function
/// binding function: Creates a binding between an HTML element and a callback function
/// - element: A null-terminated string identifying the HTML element(s) to bind to
/// - callback: A function to be called when the bound event triggers
///
/// This function performs compile-time validation on the callback function to ensure it:
/// 1. Is a proper function (not another type)
/// 2. Returns void
/// 3. Is not generic
/// 4. Does not use variable arguments
///
/// The callback function can accept various parameter types:
/// - Event: Gets the full event object
/// - *Event: Gets a pointer to the event object
/// - Other parameters will be automatically converted from event arguments in order:
///   * bool: Converted from event argument bool value
///   * int types: Converted from event argument integer value
///   * float types: Converted from event argument float value
///   * [:0]const u8: For null-terminated string data from event
///   * [*]const u8: For raw pointer data from event
///
/// Note: Event and *Event parameters do not consume argument indices from the event,
/// but all other parameter types will consume arguments in the order they appear.
///
/// Returns:
/// - The binding ID that can be used to unbind later
pub fn binding(self: webui, element: [:0]const u8, comptime callback: anytype) !usize

For how to use it, you can check the example call_zig_from_js

@jinzhongjia jinzhongjia requested a review from Copilot April 28, 2025 17:39
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request introduces enhancements to the binding function by leveraging Zig's compile-time features to improve reflection capabilities. The changes include adding documentation links to guide users toward a clearer architectural overview.

  • Added a new reference link in the API documentation of the README.md.
Files not reviewed (2)
  • examples/call_zig_from_js/main.zig: Language not supported
  • src/webui.zig: Language not supported

@jinzhongjia jinzhongjia changed the title feat: Complete binding function feat: API binding completely May 1, 2025
@jinzhongjia jinzhongjia marked this pull request as ready for review May 1, 2025 16:27
@jinzhongjia jinzhongjia merged commit 602f149 into main May 1, 2025
18 of 21 checks passed
@jinzhongjia jinzhongjia deleted the binding branch May 9, 2025 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants